package com.czl.module_storehouse.activity.inventory.profit;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TextView;

import com.czl.base.data.bean.tengyun.ProductBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.utils.GlideUtils;
import com.czl.module_base.utils.NumberUtils;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.bean.InventoryProductStorageBean;
import com.czl.module_storehouse.databinding.ActivityInventoryProfitBinding;
import com.czl.module_storehouse.event.PanSurplusEvent;
import com.czl.module_storehouse.event.ProductBeanEvent;
import com.czl.module_storehouse.mvp.presenter.InventoryPresenter;
import com.czl.module_storehouse.mvp.view.InventoryView;
import com.czl.module_base.widget.timeselector.TimeSelector;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Date;
import java.util.List;

/**
 * 盘盈入库
 *
 * @author xubin
 */
public class InventoryProfitActivity extends BaseActivity<ActivityInventoryProfitBinding> implements InventoryView {

    private final InventoryProductStorageBean mBean = new InventoryProductStorageBean();
    private SortBean mSortBean;
    @InjectPresenter
    InventoryPresenter mInventoryPresenter;

    @Override
    protected ActivityInventoryProfitBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityInventoryProfitBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mToolBinding.toolbarContentTitle.setText("盘盈登记");
        binding.tvDate.setOnClickListener(v -> showTimeSelector(binding.tvDate));
        binding.btnSubmit.setOnClickListener(v -> submit());
        binding.countView.setMaxCount(Integer.MAX_VALUE);
        EventBus.getDefault().register(this);
    }

    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(PanSurplusEvent event) {
        SortBean sortBean = event.getSortBean();
        mSortBean = sortBean;
        GlideUtils.loadImage(getContext(), binding.svItem.ivGoods, sortBean.getSortImg(), R.mipmap.pic_default_loading, R.mipmap.pic_default_error);
        binding.svItem.tvGoodsName.setText(sortBean.getSortName());
        binding.svItem.tvGoodsCode.setText("编号：" + sortBean.getSortCode());
        binding.svItem.tvGoodsNorm.setText("规格：" + sortBean.getSortModel());
    }

    private void showTimeSelector(TextView textView) {
        TimeSelector timeSelector = new TimeSelector(this, time ->
        {
            textView.setText(time);
            mBean.setManufactureDate(time);
        }, "1990-01-01", "2099-12-31", TimeSelector.FORMAT_STR2, new Date());
        timeSelector.setScrollUnit(TimeSelector.SCROLLTYPE.YEAR,
                TimeSelector.SCROLLTYPE.MONTH,
                TimeSelector.SCROLLTYPE.DAY);
        timeSelector.show();
    }

    private void submit() {
        if (mSortBean != null) {
            mBean.setSortId(mSortBean.getSortId() == null ? 0 : mSortBean.getSortId());
            mBean.setSortModel(mSortBean.getSortModel());
            List<ProductBean> productList = mSortBean.getProductList();
            if (productList != null && productList.size() > 0) {
                ProductBean productBean = productList.get(0);
                mBean.setStorehouseLocatId(productBean.getStorehouseLocatId());
                mBean.setInventoryId(productBean.getInventoryId());
            }
        }
        String count = binding.countView.mEtQuantity.getText().toString();
        if (!NumberUtils.isDouble(count)) {
            showToast("入库数量应大于0");
            return;
        }
        if (Integer.parseInt(count) <= 0) {
            showToast("入库数量应大于0");
            return;
        }
        mBean.setStorageNum(Integer.parseInt(count));
        String price = binding.etPrice.getText().toString();
        if (NumberUtils.isDouble(price)) {
            double v = Double.parseDouble(price);
            mBean.setPrice(v);
        } else {
            showToast("请输入正确的价格");
            return;
        }
        String vendor = binding.etVendor.getText().toString();
//        if (TextUtils.isEmpty(vendor)) {
//            showToast("请输入生产厂商");
//            return;
//        }
        mBean.setManufacturer(vendor);
        String date = binding.tvDate.getText().toString();
//        if (TextUtils.isEmpty(date)) {
//            showToast("请选择生产日期");
//            return;
//        }
        mBean.setManufactureDate(date);
        if (mInventoryPresenter != null) {
            mInventoryPresenter.inventoryProductStorage(mBean);
        }
    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> t) {
        super.showHttpResponse(t);
        showToast("提交成功");
        ListBean<ProductBean> listBean = (ListBean<ProductBean>) t.getData();
        if (listBean == null) {
            return;
        }
        List<ProductBean> list = listBean.getList();
        ProductBeanEvent event = new ProductBeanEvent(list);
        EventBus.getDefault().postSticky(event);
        finish();
    }
}
